package com.higgins.eventtracking.client.health.fundamentals;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.higgins.eventtracking.client.EventTrackingService;
import com.higgins.eventtracking.client.EventTrackingServiceAsync;
import com.higgins.eventtracking.client.health.components.GenericChartPanel;
import com.higgins.eventtracking.client.health.components.GenericTabInterface;

public class FrameOfMindTab extends VerticalPanel implements GenericTabInterface	{
	private final EventTrackingServiceAsync greetingService = GWT
										.create(EventTrackingService.class);
	HorizontalPanel rowOne = new HorizontalPanel();
	HorizontalPanel rowTwo = new HorizontalPanel();
	HorizontalPanel rowThree = new HorizontalPanel();
	HorizontalPanel rowFour = new HorizontalPanel();
	HorizontalPanel rowFive = new HorizontalPanel();
	HorizontalPanel rowSix = new HorizontalPanel();

	private Label daysSinceLegalCareer = new Label();
	private Label daysSinceBuildForFuture = new Label();
	private Label daysSinceHouseKeeping = new Label();
	private Label daysSinceCurrentCareer = new Label();
	private Label daysSinceRelax = new Label();
	private Label daysSinceBored = new Label();
	private Label daysSinceTakeOverTheWorld = new Label();
	private Label daysSinceDepressed = new Label();
	private Label daysSinceAnxious = new Label();
	private Label daysSinceRelationship = new Label();

	private boolean firstRun = true;
	
	private Fundamentals parentTab = null;
	
	private String tabTitle = null;
	private Integer tabIndex = null;
	private Integer height = 20;
	
	public FrameOfMindTab(Fundamentals parentTab)	{
		this.parentTab = parentTab;
		setHeight("20em");
		setWidth("800px");

	}
	
	public FrameOfMindTab()	{
		
	}
	
	public void update()	{
		if (firstRun)
			initialise();
	}
	public void initialise()	{
		setLabelWidths();
		setLabelHeights();
		addFields();
		populateFields();
		firstRun = false;
	}
	public void addFields()	{
		addRowOne();
		addRowTwo();
		addRowThree();
		addRowFour();
		addRowFive();
		addRowSix();
//		setHeight("100%");
	}
	
	private void addRowOne()	{
		rowOne.setSpacing(3);
		
		rowOne.add(daysSinceLegalCareer);
		setCellHeight(daysSinceLegalCareer, Integer.toString(daysSinceLegalCareer.getElement().getOffsetHeight()) +"px");

		rowOne.add(daysSinceAnxious);
		setCellHeight(daysSinceAnxious, Integer.toString(daysSinceAnxious.getElement().getOffsetHeight()) +"px");

		add(rowOne);
	}
	private void addRowTwo()	{
		rowTwo.setSpacing(3);
		
		rowTwo.add(daysSinceBuildForFuture);
		setCellHeight(daysSinceBuildForFuture, Integer.toString(daysSinceBuildForFuture.getElement().getOffsetHeight()) +"px");
		
		rowTwo.add(daysSinceDepressed);
		setCellHeight(daysSinceDepressed, Integer.toString(daysSinceDepressed.getElement().getOffsetHeight()) +"px");
		
		add(rowTwo);
	}
	private void addRowThree()	{
		rowThree.setSpacing(3);
		
		rowThree.add(daysSinceHouseKeeping);
		setCellHeight(daysSinceHouseKeeping, Integer.toString(daysSinceHouseKeeping.getElement().getOffsetHeight()) +"px");
		
		rowThree.add(daysSinceBored);
		setCellHeight(daysSinceBored, Integer.toString(daysSinceBored.getElement().getOffsetHeight()) +"px");

		add(rowThree);
	}

	private void addRowFour()	{
		rowFour.setSpacing(3);
		
		rowFour.add(daysSinceCurrentCareer);
		setCellHeight(daysSinceCurrentCareer, Integer.toString(daysSinceCurrentCareer.getElement().getOffsetHeight()) +"px");
		
		add(rowFour);
	}
	private void addRowFive()	{
		rowFive.setSpacing(3);
		
		rowFive.add(daysSinceRelax);
		setCellHeight(daysSinceRelax, Integer.toString(daysSinceRelax.getElement().getOffsetHeight()) +"px");
		
		add(rowFive);
	}
	private void addRowSix()	{
		rowSix.setSpacing(3);
		
		rowSix.add(daysSinceRelationship);
		setCellHeight(daysSinceRelationship, Integer.toString(daysSinceRelationship.getElement().getOffsetHeight()) +"px");
		
		add(rowSix);
	}

	private void setLabelWidths()	{
		daysSinceLegalCareer.setWidth("250px");
		daysSinceBuildForFuture.setWidth("250px");
		daysSinceHouseKeeping.setWidth("250px");
		daysSinceCurrentCareer.setWidth("250px");
		daysSinceRelax.setWidth("250px");
		daysSinceBored.setWidth("250px");
		daysSinceTakeOverTheWorld.setWidth("250px");
		daysSinceDepressed.setWidth("250px");
		daysSinceAnxious.setWidth("250px");
		daysSinceRelationship.setWidth("250px");
	}	
	private void setLabelHeights()	{
		daysSinceLegalCareer.setHeight(height + "px");
		daysSinceBuildForFuture.setHeight(height + "px");
		daysSinceHouseKeeping.setHeight(height + "px");
		daysSinceCurrentCareer.setHeight(height + "px");
		daysSinceRelax.setHeight(height + "px");
		daysSinceBored.setHeight(height + "px");
		daysSinceTakeOverTheWorld.setHeight(height + "px");
		daysSinceDepressed.setHeight(height + "px");
		daysSinceAnxious.setHeight(height + "px");
		daysSinceRelationship.setHeight(height + "px");
	}
	private void populateFields()	{
		populateDaysSinceLegalCareer();
		populateDaysSinceBuildForFuture();
		populateDaysSinceHouseKeeping();
		populateDaysSinceCurrentCareer();
		populateDaysSinceRelax();
		populateDaysSinceBored();
		populateDaysSinceAnxious();
		populateDaysSinceDepressed();
		populateDaysSinceTakeOverTheWorld();
		populateDaysSinceRelationship();
	}
	private void populateDaysSinceLegalCareer()	{
		greetingService.retrieveDaysSinceFrameOfMind("Legal Career",
				new AsyncCallback<Integer>() {
					public void onFailure(Throwable caught) {
					}
					public void onSuccess(final Integer results) {
						daysSinceLegalCareer.setText(results.toString() + " days since Legal Career");
						if (results!=null && results > 6)	{
							daysSinceLegalCareer.addStyleName("highlightAlertLabel");
							parentTab.updateTabTitle(tabIndex, tabTitle, true);
						}	
					}
				});
	}
	private void populateDaysSinceBuildForFuture()	{
		greetingService.retrieveDaysSinceFrameOfMind("Build For The Future",
				new AsyncCallback<Integer>() {
					public void onFailure(Throwable caught) {
					}
					public void onSuccess(final Integer results) {
						daysSinceBuildForFuture.setText(results.toString() + " days since Build For Future");
						if (results!=null && results > 6)	{
							daysSinceBuildForFuture.addStyleName("highlightAlertLabel");
							parentTab.updateTabTitle(tabIndex, tabTitle, true);
						}
					}
				});
	}
	private void populateDaysSinceRelationship()	{
		greetingService.retrieveDaysSinceFrameOfMind("Relationship",
				new AsyncCallback<Integer>() {
					public void onFailure(Throwable caught) {
					}
					public void onSuccess(final Integer results) {
						daysSinceRelationship.setText(results.toString() + " days since Relationship");
						if (results!=null && results > 6)	{
							daysSinceRelationship.addStyleName("highlightAlertLabel");
							parentTab.updateTabTitle(tabIndex, tabTitle, true);
						}
					}
				});
	}
	private void populateDaysSinceHouseKeeping()	{
		greetingService.retrieveDaysSinceFrameOfMind("House Keeping",
				new AsyncCallback<Integer>() {
					public void onFailure(Throwable caught) {
					}
					public void onSuccess(final Integer results) {
						daysSinceHouseKeeping.setText(results.toString() + " days since House Keeping");
						if (results!=null && results > 6)	{
							daysSinceHouseKeeping.addStyleName("highlightAlertLabel");
							parentTab.updateTabTitle(tabIndex, tabTitle, true);
						}
					}
				});
	}
	private void populateDaysSinceTakeOverTheWorld()	{
		greetingService.retrieveDaysSinceFrameOfMind("Take Over The World",
				new AsyncCallback<Integer>() {
					public void onFailure(Throwable caught) {
					}
					public void onSuccess(final Integer results) {
						daysSinceTakeOverTheWorld.setText(results.toString() + " days since Take Over The World");
						if (results!=null && results > 6)	{
							daysSinceTakeOverTheWorld.addStyleName("highlightAlertLabel");
							//parentTab.updateTabTitle(tabIndex, tabTitle, true);
						}
					}
				});
	}
	private void populateDaysSinceCurrentCareer()	{
		greetingService.retrieveDaysSinceFrameOfMind("Current Career", 
				new AsyncCallback<Integer>() {
					public void onFailure(Throwable caught) {
					}
					public void onSuccess(final Integer results) {
						daysSinceCurrentCareer.setText(results.toString() + " days since Current Career");
						if (results!=null && results > 6)	{
							daysSinceCurrentCareer.addStyleName("highlightAlertLabel");
							parentTab.updateTabTitle(tabIndex, tabTitle, true);
						}
					}
				});
	}
	private void populateDaysSinceRelax()	{
		greetingService.retrieveDaysSinceFrameOfMind("Relax", 
				new AsyncCallback<Integer>() {
					public void onFailure(Throwable caught) {
					}
					public void onSuccess(final Integer results) {
						daysSinceRelax.setText(results.toString() + " days since Relax");
						if (results!=null && results > 6)	{
							daysSinceRelax.addStyleName("highlightAlertLabel");
							parentTab.updateTabTitle(tabIndex, tabTitle, true);
						}					}
				});
	}
	private void populateDaysSinceBored()	{
		greetingService.retrieveDaysSinceFrameOfMind("Bored", 
				new AsyncCallback<Integer>() {
					public void onFailure(Throwable caught) {
					}
					public void onSuccess(final Integer results) {
						daysSinceBored.setText(results.toString() + " days since Bored");
					}
				});
	}
	private void populateDaysSinceAnxious()	{
		greetingService.retrieveDaysSinceFrameOfMind("Anxious",
				new AsyncCallback<Integer>() {
					public void onFailure(Throwable caught) {
					}
					public void onSuccess(final Integer results) {
						daysSinceAnxious.setText(results.toString() + " days since Anxious");
					}
				});
	}
	private void populateDaysSinceDepressed()	{
		greetingService.retrieveDaysSinceFrameOfMind("Depressed",
				new AsyncCallback<Integer>() {
					public void onFailure(Throwable caught) {
					}
					public void onSuccess(final Integer results) {
						daysSinceDepressed.setText(results.toString() + " days since Depressed");
					}
				});
	}

	@Override
	public String getTabTitle() {
		return tabTitle;
	}

	@Override
	public void setTabTitle(String tabTitle) {
		this.tabTitle = tabTitle;
	}

	@Override
	public Integer getTabIndex() {
		return tabIndex;
	}

	@Override
	public void setTabIndex(Integer tabIndex) {
		this.tabIndex = tabIndex;
	}

	@Override
	public void addChartPanel(GenericChartPanel chartPanel) {
		// TODO Auto-generated method stub
		
	}
}
